package com.woniuxy.finance.controller;

import com.woniuxy.finance.model.dto.EmailDto;
import com.woniuxy.finance.util.MinioUtil;
import com.woniuxy.model.entity.Goods;
import com.woniuxy.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/file")
public class ImageController {
    @Resource
    private MinioUtil minioUtil;


    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public ResponseResult<String> upload(MultipartFile file) {
         System.out.println(file.getOriginalFilename()+"上传图片的名字");
        MultipartFile[] files = {file};
        List<String> names = minioUtil.upload(files);
        System.out.println(names);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",names.get(0));
    }

    /**
     * 发送邮件
     */
    @PostMapping("/email")
    public ResponseResult<Boolean> email(@RequestBody EmailDto dto){
        String rscId = "test";
        String content="<html><body>这是有图片的邮件：<img src=\'cid:" + rscId + "\' ></body></html>";
        System.out.println(dto.getEmail());
        //dto.getGoods().forEach(System.out::println);
        for (Goods goods : dto.getGoods()) {
            String rscPath = "D:\\" + goods.getPicture();//获取产品图片名
//            MimeMessage mimeMessage = mailSender.createMimeMessage();
//            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
//            //内容
//            helper.setText("您好！，热卖商品"+"  "+"商品名:"+goodsService.getById(pull.getGoodsId()).getGoodsName() + content,true);
//            //邮件接收者
//            helper.setTo("1446218516@qq.com");
//            //邮件发送者，必须和配置文件里的一样，不然授权码匹配不上
//            helper.setFrom("1483854811@qq.com");
//
//            FileSystemResource res = new FileSystemResource(new File(rscPath));
//            helper.addInline(rscId, res);
//            mailSender.send(mimeMessage);
        }
        return null;
    }
}